package com.lipy.dubboconsumer.tool;

import com.lipy.dubbocommon.entity.Staff;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class SpelExpressionTools {

    public static String readExpr(String expr, Object data){
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression(expr);
        EvaluationContext standardEvaluationContext = new StandardEvaluationContext();
        standardEvaluationContext.setVariable("staff",data);
        String value = expression.getValue(standardEvaluationContext,String.class);
        return value;
    }

    public static void main(String[] args) {
        Staff staff = new Staff();
        staff.setId(12);
        staff.setName("lip");
        String s = readExpr("#staff.id", staff);
        System.out.println("s = " + s);
    }
}
